﻿using System;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class public_html_NewSite_CMSV2_NewsAdd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ReviewerMySql SQL = new ReviewerMySql();
        if (!IsPostBack)
        {
            for (int x = 0; x < ListRelations.Items.Count; x++)
            {
                ListRelations.Items.RemoveAt(x);
            }

            OdbcDataReader MainReader = SQL.ExecuteReader("SELECT * FROM V2_Nyhed WHERE NewsID = '" + Request.QueryString["id"] + "'");
            MainReader.Read();

            TxtID.Text = Request.QueryString["id"];
            TxtHeader.Text = MainReader["Header"].ToString();
            TxtKilde.Text = MainReader["Kilde"].ToString();
            TxtTekst.Text = MainReader["Tekst"].ToString();
            TxtTitel.Text = MainReader["Titel"].ToString();
            
            string Path = Server.MapPath("../news/");
            if(File.Exists(@Path + Request.QueryString["id"] + ".jpg")) 
            {
                DivImage.InnerHtml = "<img src=\"../news/" + Request.QueryString["id"] + ".jpg\" width=\"200px\" border=\"1\">";
            }

            if (MainReader["Screens"].ToString() == "true")
            {
                ChkScreens.Checked = true;
            }

            for (int i = 0; i < DropEmne.Items.Count; i++)
            {
                if (DropEmne.Items[i].Text.ToString() == MainReader["Emne"].ToString())
                {
                    DropEmne.Items[i].Selected = true;
                }
            }

            OdbcDataReader reader = SQL.ExecuteReader("SELECT Titel AS SpilTitel, SpilID AS SpilSpilID FROM V2_Spil GROUP BY SpilID ORDER BY Titel");
            while (reader.Read())
            {
                ListGames.Items.Add(new ListItem(reader["SpilTitel"].ToString(), reader["SpilSpilID"].ToString()));
            }

            string TempTitel = "";

            OdbcDataReader RelReader = SQL.ExecuteReader("SELECT V2_NewsRelations.*, V2_Spil.Titel  FROM V2_NewsRelations JOIN V2_Spil ON V2_Spil.SpilID = V2_NewsRelations.SpilID WHERE NewsID = '" + Request.QueryString["id"] + "'");
            while (RelReader.Read())
            {
                if (TempTitel != RelReader["Titel"].ToString())
                {
                    TempTitel = RelReader["Titel"].ToString();
                    ListRelations.Items.Add(new ListItem(RelReader["Titel"].ToString(), RelReader["SpilID"].ToString()));
                }
            }

            for (int y = 0; y < ListFormat.Items.Count; y++)
            {
                OdbcDataReader FormatReader = SQL.ExecuteReader("SELECT * FROM V2_NewsFormat WHERE NewsID = '" + Request.QueryString["id"] + "' AND Format = '" + ListFormat.Items[y].Value.ToString() + "'");
                while (FormatReader.Read())
                {
                    ListFormat.Items[y].Selected = true;
                }
                FormatReader.Close();
                FormatReader.Dispose();
            }
        }
    }

    protected void BtmSave_Click(object sender, EventArgs e)
    {
        ReviewerMySql SQL = new ReviewerMySql();
        
        SQL.ExecuteNonQuery("UPDATE V2_Nyhed SET Titel = '" + TxtTitel.Text + "', Header = '" + TxtHeader.Text + "', Tekst = '" + TxtTekst.Text + "', Kilde = '" + TxtKilde.Text + "', Screens = '" + ChkScreens.Checked.ToString() + "', Emne = '" + DropEmne.SelectedItem.ToString() + "' WHERE NewsID = '" + Request.QueryString["id"] + "'");
        
        SQL.ExecuteNonQuery("DELETE FROM V2_NewsRelations WHERE NewsID = '" + Request.QueryString["id"] + "'");
        foreach (ListItem item in ListRelations.Items)
        {
            string NewsID = TxtID.Text;
            string SpilID = item.Value.ToString();
            SQL.ExecuteNonQuery("INSERT INTO V2_NewsRelations(NewsID, SpilID) VALUES('" + NewsID + "','" + SpilID + "')");
        }

        SQL.ExecuteNonQuery("DELETE FROM V2_NewsFormat WHERE NewsID = '" + Request.QueryString["id"] + "'");
        for (int i = 0; i < ListFormat.Items.Count; i++)
        {
            if (ListFormat.Items[i].Selected)
            {
                SQL.ExecuteNonQuery("INSERT INTO V2_NewsFormat(Format, NewsID) VALUES('" + ListFormat.Items[i].Value.ToString() + "','" + TxtID.Text + "')");
            }
        }

        if (FileUploadNews.HasFile)
        {
            string Path = Server.MapPath("../news/");
            FileUploadNews.SaveAs(@Path + TxtID.Text + ".jpg");
        }

        Response.Redirect("Message.aspx?MSG=Nyheden blev redigeret.");
    }

    protected void Add_Click(object sender, EventArgs e)
    {
        string Titel = ListGames.SelectedItem.ToString();
        string SpilID = ListGames.SelectedValue.ToString();
        ListRelations.Items.Add(new ListItem(Titel, SpilID));
        ListGames.Items.RemoveAt(ListGames.SelectedIndex);
    }

    protected void Remove_Click(object sender, EventArgs e)
    {
        ListGames.Items.Add(new ListItem(ListRelations.SelectedItem.ToString(), ListRelations.SelectedValue));
        ListRelations.Items.RemoveAt(ListRelations.SelectedIndex);
    }

    protected void btmDelete_Click(object sender, EventArgs e)
    {
        ReviewerMySql SQL = new ReviewerMySql();
        SQL.ExecuteNonQuery("DELETE FROM V2_NewsRelations WHERE NewsID = '" + Request.QueryString["id"] + "'");
        SQL.ExecuteNonQuery("DELETE FROM V2_NewsFormat WHERE NewsID = '" + Request.QueryString["id"] + "'");
        SQL.ExecuteNonQuery("DELETE FROM V2_Nyhed WHERE NewsID = '" + Request.QueryString["id"] + "'");
        Response.Redirect("Message.aspx?MSG=Nyheden og alle relationer blev slettet.");
    }
}
